%% 从文本文件中导入数据
% 用于从以下文本文件中导入数据的脚本:
%
%    filename: F:\Documents\CST\s21.txt
%
% 由 MATLAB 于 2021-01-31 14:24:25 自动生成

%% Set up the Import Options and import the data
opts = delimitedTextImportOptions("NumVariables", 3);

% 指定范围和分隔符
opts.DataLines = [4, Inf];
opts.Delimiter = "\t";

% 指定列名称和类型
opts.VariableNames = ["noname", "VarName2", "VarName3"];
opts.VariableTypes = ["double", "double", "double"];

% 指定文件级属性
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";

% 导入数据
s21 = readtable("F:\Documents\CST\s21.txt", opts);
s11 = readtable("F:\Documents\CST\s11.txt", opts);

%% 转换为输出类型
s21 = table2array(s21);
s11 = table2array(s11);

%% 清除临时变量
clear opts



%%
S11 = s11(:,2)+1j*s11(:,3);
S21 = s21(:,2)+1j*s21(:,3);
fenzi = (1+S11).^2-S21.^2;
fenmu= (1-S11).^2-S21.^2;
z = sqrt(fenzi./fenmu);
zreal = real(z);
zimag = imag(z);
fre = s11(:,1);

%%
difference_real = zreal - 1;
difference_imag = zimag;
[m1] = find(abs(difference_real)<.18);
[m2] = find(abs(difference_imag)<.3);
pls = intersect(m1,m2);hold on;
% for each = 1:length(pls)
%     this_pls = pls(each);
%     plot([fre(this_pls),fre(this_pls)],[0,1.5],'color',QX(3,:));
% end
% plot([fre(pls),fre(pls)],[0,1.5],'color',QX(3,:));

pst = [fre(pls),fre(pls)];
height = repmat([ 0 1],length(pls(:,1)),1);
load color_QX;
general = {'figure name', ['改变参数'];
    'title name',' ';
    'x_label','Frequency (THz)';
    'y_label','Imdepance';
    };
close all;
data = {'x', 'y','legend','color','LineStyle';
    fre,	zreal,	'Re(Z)',QX(1,:),'-';
    fre,	zimag,	'Im(Z)',QX(6,:),'--';
%     fre,	ones(size(fre)),'off',QX(3,:),'-.';
%     fre,	zeros(size(fre)),'off',QX(3,:),'-.';
    };
hand = YW_Plot1(general,data);
% plot(pst',height','color',QX(5,:),'linestyle','-.','linewidth',1.5)
set(gca(hand),'XLim',[.75 1.9]);
set(gca,'ylim',[-0.5 2.5])